/*
 * Inici.java
 *
 */
package org.wooxes.client;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;

import java.util.Vector;

/**
 * Pàgina inicial
 */
public class Inici extends Sink {
    
    public static SinkInfo init() {
        return new SinkInfo("Inici", "Pàgina d'inici") {
            public Sink createInstance() {
                return new Inici();
            }
        };
    }

    final String TXT_ERROR_BD = "Error: No es pot conectar amb la base de dades";
    final String TXT_NO_RESULTS = "No s'ha trobat cap anunci";
    final String TXT_LOADING = "Carregant...";
    Label lblcarregant;
    SimplePanel contenedor;
    
    public Inici() {
        VerticalPanel vp = new VerticalPanel();
        vp.setWidth("100%");
        
        contenedor = new SimplePanel();
        contenedor.setStyleName("Contenedor");
        
        lblcarregant = new Label(TXT_LOADING);
        lblcarregant.setStyleName("Label-Carregant");

        vp.add(lblcarregant);
        vp.add(contenedor);
      
        initWidget(vp);
        getService().getAnuncis(mostrarAnuncis());
    }
  
    
    private AsyncCallback mostrarAnuncis(){
        return new AsyncCallback() {
            public void onSuccess(Object result){
                if(result == null){
                    lblcarregant.setVisible(false);

                    Label lblerror= new Label(TXT_ERROR_BD);
                    lblerror.setStyleName("Label-Error");
                    
                    contenedor.setWidget(lblerror);
                    return;
                }
                Vector v = (Vector) result;
                if (v.size() == 0){
                    lblcarregant.setVisible(false);
                    
                    Label lblerror = new Label(TXT_NO_RESULTS);
                    lblerror.setStyleName("Label-Error");

                    contenedor.setWidget(lblerror);
                    return;                    
                }
                
                VerticalPanel vp = new VerticalPanel();
                for(int i=0; i<v.size(); i++){
                    Anunci a = (Anunci) v.get(i);
                    vp.add(crearNoticia(a));
                    
                }

                lblcarregant.setVisible(false);
                contenedor.setWidget(vp);
            }
            
            public void onFailure(Throwable caught){
                onSuccess(null);
            }
        };
    }
    
    private VerticalPanel crearNoticia(Anunci a){
        VerticalPanel vp = new VerticalPanel();
        vp.setStyleName("Anunci-Taula");
        
        Label titol = new Label(a.getTitol());
        titol.addStyleName("Anunci-Titol");
        
        Label data = new Label(a.getFormattedData());
        data.addStyleName("Anunci-SubTitol");

        HTML text = new HTML(a.getText());
        text.addStyleName("Anunci-Text");
        text.setWordWrap(true);
        
        // S'afegeixen al panell
        vp.add(titol);
        vp.add(data);
        vp.add(text);

        return vp;
    }
    
    private static AnuncisServiceAsync getService(){
        AnuncisServiceAsync service = (AnuncisServiceAsync) GWT.create(AnuncisService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        String moduleRelativeURL = GWT.getModuleBaseURL() + "anuncis_service";
        endpoint.setServiceEntryPoint(moduleRelativeURL);
        return service;
    }
}
